//
//  BCMyFavourateViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCMyFavourateViewController.h"
#import "RDVTabBarController.h"
#import "BCOrderSecondSiderCollectionViewCell.h"
#import "BCStuffThirdTableCell.h"
#import "BCStuffInfoViewController.h"
#import "BCOrderViewController.h"

@interface BCMyFavourateViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource, actionDelegate, shopCarDelegate>
//@property (nonatomic, strong) UIView *payView;
//@property (nonatomic, strong) UILabel *countLable;
//@property (nonatomic, strong) UILabel *priceLable;
//@property (nonatomic, strong) UIButton *payButton;
//@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UICollectionView *siderCollectionView;
@property (nonatomic,strong) CALayer *dotLayer;
@property (nonatomic,assign) CGFloat endPointX;
@property (nonatomic,assign) CGFloat endPointY;
@property (nonatomic,strong) UIBezierPath *path;
@end

@implementation BCMyFavourateViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
//    [self p_initUnderBar];
    [self p_initMainView];
    [self p_initDiscriptionLable];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;
    [BCShopCarManager sharedInstance].delegate = self;
    [[BCShopCarManager sharedInstance] showShopWindow];
    [BCShopCarManager sharedInstance].butTitle = @"下订单";

    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

- (void)pushWithUUID:(NSString *)uuid{
    ;
}

#pragma mark - shop car
- (void)goTo
{
    BCOrderViewController *secondViewController = [[BCOrderViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"我的收藏";
}


-(void)p_initDiscriptionLable {
    UILabel *discriptionLable = [[UILabel alloc] initWithFrame:CGRectMake(self.siderCollectionView.frame.size.width, 0, self.mainTableView.frame.size.width, 28.f)];
    discriptionLable.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    discriptionLable.textColor = [UIColor darkGrayColor];
    discriptionLable.text = @"  标价：为邦采推荐价格";
    discriptionLable.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:discriptionLable];
}

-(void)p_initMainView {
//    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
//    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.2f, 28.f, self.view.frame.size.width-(self.view.frame.size.width*0.2), self.view.frame.size.height-28.f) collectionViewLayout:flowLayout];
//    self.mainCollectionView.delegate = self;
//    self.mainCollectionView.dataSource = self;
//    //设置contentInset属性（上左下右的值）
//    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, self.navigationController.navigationBar.frame.size.height+20.f, 0);
//    self.mainCollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
//    [self.view addSubview:self.mainCollectionView];
    
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.2f, 28.f, self.view.frame.size.width-(self.view.frame.size.width*0.2), self.view.frame.size.height-28.f-49.f) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, self.navigationController.navigationBar.frame.size.height+20.f, 0);
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.mainTableView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.mainTableView registerClass:[BCStuffThirdTableCell class] forCellReuseIdentifier:@"thirdReuse"];
    [self.view addSubview:self.mainTableView];
    ////////////////////////////////////////////////////
    
    UICollectionViewFlowLayout *sider_flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.siderCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.mainTableView.frame.origin.x, self.view.frame.size.height-49.f) collectionViewLayout:sider_flowLayout];
    self.siderCollectionView.delegate = self;
    self.siderCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.siderCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.siderCollectionView registerClass:[BCOrderSecondSiderCollectionViewCell class] forCellWithReuseIdentifier:@"SecondSiderCell"];
    self.siderCollectionView.backgroundColor = [UIColor colorWithRed:0.941 green:0.937 blue:0.941 alpha:1.000];
    [self.view addSubview:self.siderCollectionView];
    
    _endPointX = (SCREEN_WIDTH/3)+(SCREEN_WIDTH/6);
    _endPointY = SCREEN_HEIGHT;
}

#pragma mark - Private Action
//- (void)payAction:(UIButton *)item {
//    [[self rdv_tabBarController] setTabBarHidden:NO animated:NO];
//    [[self rdv_tabBarController] setSelectedIndex:1];
//    [self.navigationController popToRootViewControllerAnimated:NO];
//}

#pragma mark - actionDelegate
-(void)pushWithData:(NSDictionary *)datadic {
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCStuffThirdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thirdReuse"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.price = 145;
    
    __weak __typeof(&*cell)weakCell =cell;
    cell.reddot = ^() {
        CGRect parentRect = [weakCell convertRect:weakCell.plus.frame toView:self.view];
        [self JoinCartAnimationWithRect:parentRect];
    };
    cell.name = [NSString stringWithFormat:@"猪肉培根肉卷%ld斤一箱", indexPath.row];
    cell.amount = 0;
    cell.minusHidden = NO;
    
    for (BCShopModel *stuff in [BCShopCarManager sharedInstance].stuffList) {
        if ([cell.name isEqualToString:stuff.name]) {
            cell.minusHidden = YES;
            cell.amount = stuff.amout;
        }
    }
    cell.delegate = self;

    
    return cell;
}

- (void)reloadData
{
    [self.mainTableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 118.f;
}

/*uitableview 滑动删除 begin*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self.infoItems removeObjectAtIndex:(indexPath.row*2)];
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  @"取消收藏";
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
/*uitableview 滑动删除 end*/

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCStuffInfoViewController *stuffVC = [[BCStuffInfoViewController alloc] init];
    stuffVC.stuff_uuid = @"345CF30E-E200-4E69-8D3E-084CFA0F7D01";
    [self.navigationController pushViewController:stuffVC animated:YES];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if (section == 0) {
        return 8;
//    }
//    else {
//        return 10;
//    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *ID = @"SecondSiderCell";
        BCOrderSecondSiderCollectionViewCell *cell = [BCOrderSecondSiderCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.title.text = @"禽蛋肉品";
            cell.backgroundColor = [UIColor whiteColor];
            cell.title.textColor = BangcaiGreen;//[UIColor darkTextColor];
        }
        else if (indexPath.row == 1) {
            cell.title.text = @"蔬菜水果";
        }
        else if (indexPath.row == 2) {
            cell.title.text = @"水产冻品";
        }
        else if (indexPath.row == 3) {
            cell.title.text = @"熟食面点";
        }
        else if (indexPath.row == 4) {
            cell.title.text = @"酒水饮料";
        }
        else if (indexPath.row == 5) {
            cell.title.text = @"调料熟食";
        }
        else if (indexPath.row == 6) {
            cell.title.text = @"米面粮油";
        }
        else {
            cell.title.text = @"厨房用品";
        }
        
        return cell;
        
//    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    if ([collectionView isEqual:self.siderCollectionView]) {
        return CGSizeMake(self.siderCollectionView.frame.size.width, 62.f);
//    }
//    else {
//        return CGSizeMake(self.mainCollectionView.frame.size.width, 118.f);
//    }

}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 25;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
//    if ([collectionView isEqual:self.mainCollectionView]) {
//        return 5.f;
//    }
//    else {
        return 0.f;
//    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *cells = [collectionView visibleCells];
    [cells enumerateObjectsUsingBlock:^(BCOrderSecondSiderCollectionViewCell*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BCOrderSecondSiderCollectionViewCell *cell = (BCOrderSecondSiderCollectionViewCell *)obj;
        cell.backgroundColor = [UIColor clearColor];
        cell.title.textColor = [UIColor darkGrayColor];
    }];
    BCOrderSecondSiderCollectionViewCell *cell = (BCOrderSecondSiderCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.title.textColor = BangcaiGreen;}

#pragma mark -加入购物车动画
-(void) JoinCartAnimationWithRect:(CGRect)rect
{
    
    CGFloat startX = rect.origin.x;
    CGFloat startY = rect.origin.y;
    
    _path= [UIBezierPath bezierPath];
    [_path moveToPoint:CGPointMake(startX, startY)];
    //三点曲线
    [_path addCurveToPoint:CGPointMake(_endPointX, _endPointY)
             controlPoint1:CGPointMake(startX, startY)
             controlPoint2:CGPointMake(startX - 180, startY - 200)];
    
    _dotLayer = [CALayer layer];
    _dotLayer.backgroundColor = [UIColor redColor].CGColor;
    _dotLayer.frame = CGRectMake(0, 0, 15, 15);
    _dotLayer.cornerRadius = (15 + 15) /4;
    [self.view.layer addSublayer:_dotLayer];
    [self groupAnimation];
    
}
#pragma mark - 组合动画
-(void)groupAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = RedAnimationDuring;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = RedAnimationDuring+0.3f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [_dotLayer addAnimation:groups forKey:nil];
    
    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:RedAnimationDuring+0.3f];
    
}
- (void)removeFromLayer:(CALayer *)layerAnimation{
    
    [layerAnimation removeFromSuperlayer];
}

@end
